#!/bin/bash
# 定义一个变量来记录容器和镜像名称
CONTAINER_NAME="user-center"
IMAGE_NAME="user-center"
NETWORK_NAME="cloud-server"
IP_ADDRESS="192.168.107.7"
VOLUME_PATH="/Volumes/SD/MuLu/logger/$CONTAINER_NAME"

# Step 1: Clean and install with Maven (using a specific profile)
echo "Running Maven clean and install with profile 'local'..."
mvn clean install -P local

# Step 2: Build the Docker image using the Dockerfile in the current directory
echo "Building Docker image..."
docker build -t $IMAGE_NAME .

# Step 3: Check if the container already exists and remove it if needed
echo "Checking for existing container..."
container_id=$(docker ps -aq -f name=$CONTAINER_NAME)
if [ -n "$container_id" ]; then
  echo "Removing existing container with ID: $container_id..."
  docker rm -f $container_id
fi

# Step 4: Run the Docker container
echo "Running the Docker container..."
docker run -d \
  --name $CONTAINER_NAME \
  --network $NETWORK_NAME \
  --ip $IP_ADDRESS \
  -p 8890:8890 \
  -v $VOLUME_PATH:/opt/ \
  $IMAGE_NAME

# Step 5: Connect the container to the bridge network
echo "Connecting the container to the bridge network..."
docker network connect bridge $CONTAINER_NAME

echo "Script execution completed successfully!"
